home *** CD-ROM | disk | FTP | other *** search
/ 3D Games - Real-time Rend…ng & Software Technology / 3D Games - Real-time Rendering & Software Technology.iso / flysdk / frontend / flyEditor / flyEditorDoc.cpp < prev    next >
Encoding:
C/C++ Source or Header  |  2000-12-12  |  10.7 KB  |  480 lines

  1. // flyEditorDoc.cpp : implementation of the CFlyEditorDoc class
  2. //
  3.  
  4. #include "stdafx.h"
  5. #include "flyEditor.h"
  6. #include "flyEditorDoc.h"
  7. #include "flyEditorView.h"
  8. #include "flyRenderView.h"
  9. #include "LeftView.h"
  10.  
  11. #ifdef _DEBUG
  12. #define new DEBUG_NEW
  13. #undef THIS_FILE
  14. static char THIS_FILE[] = __FILE__;
  15. #endif
  16.  
  17. extern CFlyRenderView *rv;
  18. extern CFlyEditorView *lv;
  19. extern CLeftView *tv;
  20.  
  21. extern int drawflag;
  22. int active_mode=0,active_num,active_tot;
  23. bsp_object *active_sel=0;
  24. extern CDialogBar *dlgBar;
  25.  
  26. /////////////////////////////////////////////////////////////////////////////
  27. // CFlyEditorDoc
  28.  
  29. IMPLEMENT_DYNCREATE(CFlyEditorDoc, CDocument)
  30.  
  31. BEGIN_MESSAGE_MAP(CFlyEditorDoc, CDocument)
  32.     //{{AFX_MSG_MAP(CFlyEditorDoc)
  33.     ON_COMMAND(ID_SIMMULATE, OnSimmulate)
  34.     ON_UPDATE_COMMAND_UI(ID_SIMMULATE, OnUpdateSimmulate)
  35.     ON_COMMAND(ID_RENDER_OPENGL, OnRenderOpengl)
  36.     ON_UPDATE_COMMAND_UI(ID_RENDER_OPENGL, OnUpdateRenderOpengl)
  37.     ON_COMMAND(ID_RENDER_NORENDER, OnRenderNorender)
  38.     ON_UPDATE_COMMAND_UI(ID_RENDER_NORENDER, OnUpdateRenderNorender)
  39.     ON_COMMAND(ID_FILE_RESTART, OnFileRestart)
  40.     ON_UPDATE_COMMAND_UI(ID_FILE_RESTART, OnUpdateFileRestart)
  41.     ON_COMMAND(ID_VIEW_OBJSCENE, OnViewObjscene)
  42.     ON_UPDATE_COMMAND_UI(ID_VIEW_OBJSCENE, OnUpdateViewObjscene)
  43.     ON_BN_CLICKED(IDC_ACTIVATE, OnActivate)
  44.     ON_BN_CLICKED(IDC_ACTIVE, OnActive)
  45.     ON_BN_CLICKED(IDC_DESTROY, OnDestroy)
  46.     ON_NOTIFY(UDN_DELTAPOS, IDC_SPIN, OnDeltaposSpin)
  47.     //}}AFX_MSG_MAP
  48. END_MESSAGE_MAP()
  49.  
  50. /////////////////////////////////////////////////////////////////////////////
  51. // CFlyEditorDoc construction/destruction
  52.  
  53. CFlyEditorDoc::CFlyEditorDoc()
  54. {
  55.     flyengine=this;
  56.     noinput=1;
  57.     tree_il.Create(IDB_TREE_ICONS,16,0,RGB(192,192,192));
  58.     appid=FLYAPPID_FLYEDITOR;
  59. }
  60.  
  61. CFlyEditorDoc::~CFlyEditorDoc()
  62. {
  63.     close_fly_file();
  64.     free_directx();
  65. }
  66.  
  67. /////////////////////////////////////////////////////////////////////////////
  68. // CFlyEditorDoc serialization
  69.  
  70. void CFlyEditorDoc::Serialize(CArchive& ar)
  71. {
  72.     if (ar.IsStoring())
  73.     {
  74.     }
  75.     else
  76.     {
  77.     }
  78. }
  79.  
  80. /////////////////////////////////////////////////////////////////////////////
  81. // CFlyEditorDoc diagnostics
  82.  
  83. #ifdef _DEBUG
  84. void CFlyEditorDoc::AssertValid() const
  85. {
  86.     CDocument::AssertValid();
  87. }
  88.  
  89. void CFlyEditorDoc::Dump(CDumpContext& dc) const
  90. {
  91.     CDocument::Dump(dc);
  92. }
  93. #endif //_DEBUG
  94.  
  95. /////////////////////////////////////////////////////////////////////////////
  96. // CFlyEditorDoc commands
  97.  
  98. BOOL CFlyEditorDoc::OnOpenDocument(LPCTSTR lpszPathName) 
  99. {
  100.     if (!CDocument::OnOpenDocument(lpszPathName))
  101.         return FALSE;
  102.  
  103.     set_engine(this,rv->m_hWnd,AfxGetInstanceHandle(),FLYAPPID_FLYEDITOR);
  104.     init_directx();
  105.  
  106.     char str[256],fly_file_name[256];
  107.     strcpy(fly_file_name,lpszPathName);
  108.     strcpy(str,flysdkpath);
  109.     strcat(str,"data\\");
  110.     int len=strlen(str);
  111.     _strlwr(fly_file_name);
  112.     if (strncmp(fly_file_name,str,len)==0)
  113.         open_fly_file(&fly_file_name[len]);
  114.     if (flyfilename[0]==0)
  115.     {
  116.         strcpy(str,"Could not load .fly file:\n");
  117.         strcat(str,fly_file_name);
  118.         AfxMessageBox(str,MB_OK|MB_ICONEXCLAMATION);
  119.     }
  120.  
  121.     FillTreeView();
  122.     SetModifiedFlag(0);
  123.  
  124.     return TRUE;
  125. }
  126.  
  127. BOOL CFlyEditorDoc::OnNewDocument()
  128. {
  129.     if (!CDocument::OnNewDocument())
  130.         return FALSE;
  131.  
  132.     set_engine(this,rv->m_hWnd,AfxGetInstanceHandle(),FLYAPPID_FLYEDITOR);
  133.     init_directx();
  134.  
  135.     close_fly_file();
  136.  
  137.     CTreeCtrl& tree=tv->GetTreeCtrl();
  138.     tree.SelectItem(0);
  139.     tree.DeleteAllItems();
  140.     
  141.     CListCtrl& list=lv->GetListCtrl();
  142.     list.DeleteAllItems();
  143.     list.DeleteColumn(0);
  144.     list.DeleteColumn(0);
  145.     list.DeleteColumn(0);
  146.  
  147.     FillTreeView();
  148.  
  149.     SetModifiedFlag(0);
  150.  
  151.     UpdateAllViews(0);
  152.  
  153.     return TRUE;
  154. }
  155.  
  156. void CFlyEditorDoc::FillTreeView()
  157. {
  158.     CTreeCtrl& tree=tv->GetTreeCtrl();
  159.     tree.SelectItem(0);
  160.     tree.DeleteAllItems();
  161.     tree.ModifyStyle(0,TVS_HASLINES|TVS_HASBUTTONS|TVS_LINESATROOT|TVS_SHOWSELALWAYS|TVS_EDITLABELS);
  162.     tree.SetImageList(&tree_il,TVSIL_NORMAL);
  163.  
  164.     CListCtrl& list=lv->GetListCtrl();
  165.     list.ModifyStyle(0,LVS_SINGLESEL);
  166.  
  167.     int i,j,k=0;
  168.     char *name;
  169.     HTREEITEM item1,item2,item3,item4,item5;
  170.  
  171.     item1=tree.InsertItem("Global");
  172.     tree.SetItemData(item1,TREE_TYPE_GLOBAL);
  173.     tree.SetItemImage(item1,TREE_TYPE_GLOBAL*2,TREE_TYPE_GLOBAL*2+1);
  174.     item2=tree.InsertItem("Plugins");
  175.     tree.SetItemData(item2,TREE_TYPE_PLUGINS);
  176.     tree.SetItemImage(item2,TREE_TYPE_PLUGINS*2,TREE_TYPE_PLUGINS*2+1);
  177.     for( i=0;i<dll.ndll;i++ )
  178.         {
  179.         item3=tree.InsertItem(dll.dll[i]->dll_filename,item2);
  180.         tree.SetItemData(item3,TREE_TYPE_DLLS);
  181.         tree.SetItemImage(item3,TREE_TYPE_DLLS*2,TREE_TYPE_DLLS*2+1);
  182.         for( j=0;j<dll.dll[i]->nclasses;j++ )
  183.             {
  184.             name=dll.cd[k++]->get_name();
  185.             item4=tree.InsertItem(name,item3);
  186.             tree.SetItemData(item4,TREE_TYPE_STOCK);
  187.             tree.SetItemImage(item4,TREE_TYPE_STOCK*2,TREE_TYPE_STOCK*2+1);
  188.             char buf[256];
  189.             int n=0;
  190.             while(1)
  191.                 {
  192.                 sprintf(buf,"%s%i",name,n++);
  193.                 bsp_object *o=get_stock_object(buf);
  194.                 if (o==0) break;
  195.                 item5=tree.InsertItem(o->long_name,item4);
  196.                 tree.SetItemData(item5,(DWORD)o);
  197.                 tree.SetItemImage(item5,TREE_TYPE_DYNOBJECT*2,TREE_TYPE_DYNOBJECT*2+1);
  198.                 }
  199.             }
  200.         }
  201.  
  202.     item2=tree.InsertItem("Objects");
  203.     tree.SetItemData(item2,TREE_TYPE_OBJECTS);
  204.     tree.SetItemImage(item2,TREE_TYPE_OBJECTS*2,TREE_TYPE_OBJECTS*2+1);
  205.     
  206.     item2=tree.InsertItem("Sounds");
  207.     tree.SetItemData(item2,TREE_TYPE_SOUNDS);
  208.     tree.SetItemImage(item2,TREE_TYPE_SOUNDS*2,TREE_TYPE_SOUNDS*2+1);
  209.     
  210.     item2=tree.InsertItem("Textures");
  211.     tree.SetItemData(item2,TREE_TYPE_TEXTURES);
  212.     tree.SetItemImage(item2,TREE_TYPE_TEXTURES*2,TREE_TYPE_TEXTURES*2+1);
  213.  
  214.     item2=tree.InsertItem("Lightmaps");
  215.     tree.SetItemData(item2,TREE_TYPE_LIGHTMAPS);
  216.     tree.SetItemImage(item2,TREE_TYPE_LIGHTMAPS*2,TREE_TYPE_LIGHTMAPS*2+1);
  217.  
  218.     tree.SelectItem(item1);
  219. }
  220.  
  221. void CFlyEditorDoc::OnSimmulate() 
  222. {
  223.     if (rv->timer==0)
  224.         rv->timer=rv->SetTimer(1,1,0);
  225.     else 
  226.         {
  227.         rv->KillTimer(rv->timer);
  228.         rv->timer=0;
  229.         }
  230. }
  231.  
  232. void CFlyEditorDoc::OnUpdateSimmulate(CCmdUI* pCmdUI) 
  233. {
  234.     pCmdUI->SetCheck(rv->timer);
  235. }
  236.  
  237. void CFlyEditorDoc::OnRenderNorender() 
  238. {
  239.     if (rv->render_type!=0)
  240.         {
  241.         rv->render_type=0;
  242.         close_texture_cache();
  243.         free_render();
  244.         }
  245.     rv->Invalidate(1);
  246. }
  247.  
  248. void CFlyEditorDoc::OnUpdateRenderNorender(CCmdUI* pCmdUI) 
  249. {
  250.     pCmdUI->SetRadio(rv->render_type==0);
  251. }
  252.  
  253. void CFlyEditorDoc::OnRenderOpengl() 
  254. {
  255.     if (rv->render_type!=1)
  256.         {
  257.         rv->render_type=1;
  258.         close_texture_cache();
  259.         init_render(FLY_RENDER_OPENGL);
  260.         init_texture_cache();
  261.         }
  262.     else OnRenderNorender();
  263.     rv->Invalidate(1);
  264. }
  265.  
  266. void CFlyEditorDoc::OnUpdateRenderOpengl(CCmdUI* pCmdUI) 
  267. {
  268.     pCmdUI->SetRadio(rv->render_type==1);    
  269. }
  270.  
  271. BOOL CFlyEditorDoc::OnSaveDocument(LPCTSTR lpszPathName) 
  272. {
  273.     char str[256],fly_file_name[256];
  274.  
  275.     strcpy(fly_file_name,lpszPathName);
  276.     strcpy(str,flysdkpath);
  277.     strcat(str,"data\\");
  278.     int len=strlen(str);
  279.  
  280.     _strlwr(fly_file_name);
  281.     if (strncmp(fly_file_name,str,len)==0)
  282.     {
  283.         if (save_fly_file(&fly_file_name[len]))
  284.         {
  285.             SetModifiedFlag(0);
  286.             return 1;
  287.         }
  288.     }
  289.  
  290.     strcpy(str,"Could not save .fly file:\n");
  291.     strcat(str,fly_file_name);
  292.     AfxMessageBox(str,MB_OK|MB_ICONEXCLAMATION);
  293.     return 0;
  294. }
  295.  
  296. void CFlyEditorDoc::OnFileRestart() 
  297. {
  298.     if (IsModified())
  299.         OnSaveDocument((char *)(LPCSTR)GetPathName());
  300.     OnOpenDocument((char *)(LPCSTR)GetPathName());
  301.     UpdateAllViews(0);
  302. }
  303.  
  304. void CFlyEditorDoc::OnUpdateFileRestart(CCmdUI* pCmdUI) 
  305. {
  306.     pCmdUI->Enable(!GetPathName().IsEmpty());
  307. }
  308.  
  309. void CFlyEditorDoc::OnViewObjscene() 
  310. {
  311.     drawflag=!drawflag;
  312.     rv->Invalidate(0);
  313. }
  314.  
  315. void CFlyEditorDoc::OnUpdateViewObjscene(CCmdUI* pCmdUI) 
  316. {
  317.     pCmdUI->SetCheck(drawflag);
  318. }
  319.  
  320. void CFlyEditorDoc::OnActivate() 
  321. {
  322.     CTreeCtrl& tree=tv->GetTreeCtrl();
  323.     HTREEITEM item=tree.GetSelectedItem();
  324.     if (item==0) return;
  325.     DWORD type=tree.GetItemData(item);
  326.     if (type>255)
  327.         flyengine->activate(((bsp_object *)type)->clone());
  328.     rv->Invalidate(0);
  329. }
  330.  
  331. void CFlyEditorDoc::find_active()
  332. {
  333.     static char str[32];
  334.  
  335.     CTreeCtrl& tree=tv->GetTreeCtrl();
  336.     HTREEITEM item=tree.GetSelectedItem();
  337.     if (item==0) return;
  338.     DWORD type=tree.GetItemData(item);
  339.  
  340.     active_num=-1;
  341.     active_tot=0;
  342.  
  343.     if (type<256)
  344.     {
  345.         active_sel=0;
  346.         dlgBar->GetDlgItem(IDC_NUM)->SetWindowText("");
  347.         return;
  348.     }
  349.  
  350.     bsp_object *o=active_obj0,*first=0;
  351.     while(o)
  352.         {
  353.         if (o->source==(bsp_object *)type)
  354.             {
  355.             if (o==active_sel)
  356.                 active_num=active_tot;
  357.             if (first==0)
  358.                 first=o;
  359.             active_tot++;
  360.             }
  361.         o=(bsp_object *)o->next_obj;
  362.         }
  363.  
  364.     if (active_num==-1 || active_sel==0)
  365.         {
  366.         active_sel=first;
  367.         if (active_tot>0)
  368.             active_num=0;
  369.         else active_num=-1;
  370.         }
  371.  
  372.     if (active_sel==0)
  373.         active_sel=(bsp_object *)type;
  374.     
  375.     if (active_tot==0)
  376.         strcpy(str,"None");
  377.     else if (active_mode==0)
  378.             sprintf(str,"%i",active_tot);
  379.         else sprintf(str,"%i/%i",active_num+1,active_tot);
  380.     dlgBar->GetDlgItem(IDC_NUM)->SetWindowText(str);
  381.  
  382.     if (active_mode==0 && type>255)
  383.         active_sel=(bsp_object *)type;
  384. }
  385.  
  386. void CFlyEditorDoc::OnActive() 
  387. {
  388.     active_mode=dlgBar->IsDlgButtonChecked(IDC_ACTIVE);
  389.     dlgBar->GetDlgItem(IDC_NUM)->EnableWindow(active_mode);
  390.     dlgBar->GetDlgItem(IDC_SPIN)->EnableWindow(active_mode);
  391.     dlgBar->GetDlgItem(IDC_ACTIVATE)->EnableWindow(active_mode);
  392.     dlgBar->GetDlgItem(IDC_DESTROY)->EnableWindow(active_mode);
  393.     lv->update_param_list();
  394.     rv->Invalidate(0);
  395. }
  396.  
  397. void CFlyEditorDoc::OnDestroy() 
  398. {
  399.     CTreeCtrl& tree=tv->GetTreeCtrl();
  400.     HTREEITEM item=tree.GetSelectedItem();
  401.     if (item==0) return;
  402.     DWORD type=tree.GetItemData(item);
  403.     
  404.     if (active_mode==1 && active_sel)
  405.     {
  406.         bsp_object *o=active_obj0,*op=0;
  407.         while(o)
  408.             {
  409.             if (o->source==(bsp_object *)type)
  410.                 if (o==active_sel)
  411.                     break;
  412.             op=o;
  413.             o=(bsp_object *)o->next_obj;
  414.             }
  415.         if (o==0) return;
  416.         
  417.         if (last_active_obj==o)
  418.             last_active_obj=op;
  419.         
  420.         bsp_object *n=(bsp_object *)o->next_obj;
  421.         delete o;
  422.         if (op==0)
  423.             {
  424.             active_obj0=n;
  425.             active_sel=n;
  426.             }
  427.         else 
  428.             {
  429.             op->next_obj=n;
  430.             active_sel=op;
  431.             }
  432.  
  433.         lv->update_param_list();
  434.         rv->Invalidate(0);
  435.     }
  436. }
  437.  
  438. void CFlyEditorDoc::OnDeltaposSpin(NMHDR* pNMHDR, LRESULT* pResult) 
  439. {
  440.     NM_UPDOWN* pNMUpDown = (NM_UPDOWN*)pNMHDR;
  441.  
  442.     CTreeCtrl& tree=tv->GetTreeCtrl();
  443.     HTREEITEM item=tree.GetSelectedItem();
  444.     if (item==0) return;
  445.     DWORD type=tree.GetItemData(item);
  446.     if (active_mode && type>255)
  447.     {
  448.     if (pNMUpDown->iDelta==-1)
  449.         {
  450.         if (active_num<active_tot-1)
  451.             active_num++;
  452.         }
  453.     else
  454.     if (pNMUpDown->iDelta==1)
  455.         {
  456.         if (active_num>0)
  457.             active_num--;
  458.         }
  459.  
  460.     active_sel=0;
  461.     int i=active_num;
  462.     bsp_object *o=active_obj0;
  463.     while(o)
  464.         {
  465.         if (o->source==(bsp_object *)type)
  466.             if (i==0)
  467.                 {
  468.                 active_sel=o;
  469.                 break;
  470.                 }
  471.             else i--;
  472.         o=(bsp_object *)o->next_obj;
  473.         }
  474.     }
  475.     lv->update_param_list();
  476.     rv->Invalidate(0);
  477.     
  478.     *pResult = 0;
  479. }
  480.